package com.pm.web.controller.projectManage;

import com.alibaba.fastjson2.JSONObject;
import com.pm.buglist.domain.SysFiles;
import com.pm.buglist.service.SysFilesService;
import com.pm.common.OAService;
import com.pm.common.annotation.Log;
import com.pm.common.config.ParamConfig;
import com.pm.common.constant.ProjectConstants;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.core.domain.entity.Attachment;
import com.pm.common.core.domain.entity.ProjectListNodeMember;
import com.pm.common.core.domain.entity.SysUser;
import com.pm.common.core.page.TableDataInfo;
import com.pm.common.enums.BusinessType;
import com.pm.common.enums.TaskStatusEnum;
import com.pm.common.utils.file.FileUtils;
import com.pm.common.utils.poi.ExcelUtil;
import com.pm.project.domain.vo.ProjectListNodeMemberVO;
import com.pm.project.domain.vo.ProjectListVO;
import com.pm.project.service.TaskService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/project/task")
public class TaskController extends BaseController {

    @Resource
    TaskService taskService;

    @Resource
    SysFilesService sysFilesService;

    @Resource
    OAService oaService;


    @GetMapping("/list")
    @Log(title = "项目:查询任务列表",businessType = BusinessType.OTHER)
    public TableDataInfo list(ProjectListVO projectListVO)
    {
        startPage();
        List<ProjectListNodeMemberVO> list = taskService.list(projectListVO);
        return getDataTable(list);
    }

    @GetMapping("/getMyProjectFile")
    @Log(title = "项目:查询任务列表",businessType = BusinessType.OTHER)
    public TableDataInfo getMyProjectFile(ProjectListVO projectListVO)
    {
        startPage();
        List<JSONObject> list = taskService.getMyProjectFile(projectListVO);
        return getDataTable(list);
    }



    @GetMapping("/queryTaskFile")
    @Log(title = "项目:查询任务附件列表",businessType = BusinessType.OTHER)
    public AjaxResult queryTaskFile(Long projectNodeMemberId)
    {
        return taskService.queryTaskFile(projectNodeMemberId);

    }

    @PostMapping("/queryPLMFile")
    @Log(title = "项目:查询项目PLM附件列表",businessType = BusinessType.OTHER)
    public AjaxResult queryPLMFile(@RequestBody JSONObject obj)
    {
        return taskService.queryPLMFile(obj);

    }


    @GetMapping("/queryPLMFileUrl")
    @Log(title = "项目:查询项目PLM附件列表",businessType = BusinessType.OTHER)
    public AjaxResult queryPLMFileUrl(String fileId)
    {
        return taskService.queryPLMFileUrl(fileId);

    }



    /**
     * 校验任务状态
     * @param
     * @return
     */
    @PostMapping("/checkTaskStart")
    @Log(title = "校验任务状态",businessType = BusinessType.OTHER)
    public AjaxResult checkTaskStart(@RequestBody JSONObject param)
    {
        return taskService.checkTaskStart(param);
    }


    /**
     * 发起OA流程项目附件审批流程
     * @param
     * @return
     */
    @PostMapping("/process/add")
    @Log(title = "OA:发起OA流程项目附件审批流程",businessType = BusinessType.OTHER)
    public AjaxResult addFileProcess(@RequestBody JSONObject param)
    {
        List<Attachment> attachments = new ArrayList<>();

        if(!CollectionUtils.isEmpty(param.getJSONArray("fileIds"))){
           List<JSONObject> fileList=  param.getJSONArray("fileIds").stream().map(item->{
                JSONObject obj = new JSONObject();
                obj.put("fileId",item);
                return obj;
            }).collect(Collectors.toList());

            param.put("files",fileList);
            taskService.addFileMember(param);
            List<SysFiles> files = sysFilesService.queryFileList(param);

            for (SysFiles file : files) {
                try{
                    Attachment attachment = new Attachment();
                    attachment.setFdFileName(file.getOriginName());
                    attachment.setPath(file.getPath());
                    attachment.setFdKey(ProjectConstants.PROCESS_ADD_FILE_KEY);
                    attachments.add(attachment);
                }catch (Exception e){
                    e.printStackTrace();
                    logger.error("文件转Base64异常:"+e.getMessage());
                }

            }
        }


        return  oaService.addFileProcess(param.getString("projectNodeMemberId"),param.getString("remark"),attachments,param.getIntValue("type"));
    }


    /**
     * 绑定PLM文件
     * @param
     * @return
     */
    @PostMapping("/bindPLMFile")
    @Log(title = "绑定PLM文件",businessType = BusinessType.OTHER)
    public AjaxResult bindPLMFile(@RequestBody JSONObject param)
    {

        if(CollectionUtils.isEmpty(param.getJSONArray("files"))){
            return AjaxResult.error("没有选择文件!");
        }

        return taskService.bindPLMFile(param);


    }

    @GetMapping("/totalTaskForUser")
    @Log(title = "项目:首页个人用户汇总",businessType = BusinessType.OTHER)
    public AjaxResult totalTaskForUser(String userFlag)
    {
        return taskService.totalTaskForUser(userFlag);

    }


    @Log(title = "导出项目进度", businessType = BusinessType.EXPORT)
    @PostMapping("/exportByProjectId")
    public void exportByProjectId(HttpServletResponse response, ProjectListVO projectListVO)
    {
        List<ProjectListNodeMemberVO> list = taskService.exportByProjectId(projectListVO);
        ExcelUtil<ProjectListNodeMemberVO> util = new ExcelUtil<>(ProjectListNodeMemberVO.class);
        util.exportExcel(response, list, "用户数据");
    }


}
